using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using BA.DAL;

namespace BlazeApps.Library.Form
{
    [Serializable()]
    public class FormResults : Base
    {
        #region " Members "

        private Guid _FormID = Guid.Empty;
        private Guid _FormQuestionID = Guid.Empty;
        private Guid _FormRowID = Guid.Empty;
        private Guid _FormColumnID = Guid.Empty;
        private Int64 _AnswerCount = 0;
        private Int64 _Divisor = 0;

        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();
        private DataRow row;

        #endregion " Members "

        #region " Properties "

        public Guid FormID
        {
            get
            {
                return _FormID;
            }
            set
            {
                _FormID = value;
            }
        }

        public Guid FormQuestionID
        {
            get
            {
                return _FormQuestionID;
            }
            set
            {
                _FormQuestionID = value;
            }
        }

        public Guid FormRowID
        {
            get
            {
                return _FormRowID;
            }
            set
            {
                _FormRowID = value;
            }
        }

        public Guid FormColumnID
        {
            get
            {
                return _FormColumnID;
            }
            set
            {
                _FormColumnID = value;
            }
        }

        public Int64 AnswerCount
        {
            get
            {
                return _AnswerCount;
            }
            set
            {
                _AnswerCount = value;
            }
        }

        public Int64 Divisor
        {
            get
            {
                return _Divisor;
            }
            set
            {
                _Divisor = value;
            }
        }

        public Decimal ResultPercent
        {
            get
            {
                if (Divisor == 0)
                {
                    return System.Math.Round((Decimal)0, 1);
                }
                else
                {
                    return System.Math.Round((Decimal)((AnswerCount / Divisor) * 100), 1);
                }
            }
        }

        #endregion

        #region " Base Methods "

        public void GetFormResult(Guid FormQuestionID)
        {
            GetData(new Criteria(FormQuestionID, Guid.Empty));
        }

        public void GetFormChoiceResult(Guid FormQuestionID, Guid FormRowID)
        {
            GetChoiceResultData(new Criteria(FormQuestionID, FormRowID));
        }

        public void GetFormMatrixSingleResult(Guid FormQuestionID, Guid FormRowID, Guid FormColumnID)
        {
            GetMatrixSingleResultData(new Criteria(FormQuestionID, FormRowID, FormColumnID));
        }

        public void GetFormMatrixMultipleAnswerResult(Guid FormQuestionID, Guid FormRowID, Guid FormColumnID)
        {
            GetMatrixMultipleResultData(new Criteria(FormQuestionID, FormRowID, FormColumnID));
        }

        #endregion

        #region " Criteria "

        [Serializable()]
        private class Criteria
        {
            public Guid FormID = Guid.Empty;
            public Guid FormQuestionID = Guid.Empty;
            public Guid FormRowID = Guid.Empty;
            public Guid FormColumnID = Guid.Empty;

            public Criteria(Guid FormID)
            {
                this.FormID = FormID;
            }

            public Criteria(Guid FormQuestionID, Guid FormRowID)
            {
                this.FormQuestionID = FormQuestionID;
                this.FormRowID = FormRowID;
            }

            public Criteria(Guid FormQuestionID, Guid FormRowID, Guid FormColumnID)
            {
                this.FormQuestionID = FormQuestionID;
                this.FormRowID = FormRowID;
                this.FormColumnID = FormColumnID;
            }
        }

        #endregion

        #region " Get "

        private void GetData(Criteria CriteriaItems)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DAL.AddParameter("@FormQuestionID", CriteriaItems.FormQuestionID);

            using (DataTable DT = DAL.GetDataTable(Procs.ListAnswerResult.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open))
            {
                if (DT.Rows.Count > 0)
                {
                    row = DT.Rows[0];

                    AnswerCount = Convert.ToInt64(row["AnswerCount"].ToString());
                    Divisor = Convert.ToInt64(row["Divisor"].ToString());
                }
            }
        }

        private void GetChoiceResultData(Criteria CriteriaItems)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DAL.AddParameter("@FormRowID", CriteriaItems.FormRowID);
            DAL.AddParameter("@FormQuestionID", CriteriaItems.FormQuestionID);

            using (DataTable DT = DAL.GetDataTable(Procs.ListFormSelectionAnswerResults.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open))
            {
                if (DT.Rows.Count > 0)
                {
                    row = DT.Rows[0];

                    AnswerCount = Convert.ToInt64(row["AnswerCount"].ToString());
                    Divisor = Convert.ToInt64(row["Divisor"].ToString());
                }
            }
        }

        private void GetMatrixSingleResultData(Criteria CriteriaItems)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DAL.AddParameter("@FormQuestionID", CriteriaItems.FormQuestionID);
            DAL.AddParameter("@FormRowID", CriteriaItems.FormRowID);
            DAL.AddParameter("@FormColumnID", CriteriaItems.FormColumnID);

            using (DataTable DT = DAL.GetDataTable(Procs.ListFormMatrixSingleAnswerResults.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open))
            {
                if (DT.Rows.Count > 0)
                {
                    row = DT.Rows[0];

                    AnswerCount = Convert.ToInt64(row["ColorID"].ToString());
                    Divisor = Convert.ToInt64(row["UserID"].ToString());
                }
            }
        }

        private void GetMatrixMultipleResultData(Criteria CriteriaItems)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DAL.AddParameter("@FormQuestionID", CriteriaItems.FormQuestionID);
            DAL.AddParameter("@FormRowID", CriteriaItems.FormRowID);
            DAL.AddParameter("@FormColumnID", CriteriaItems.FormColumnID);

            using (DataTable DT = DAL.GetDataTable(Procs.ListFormMatrixMultipleAnswerResults.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open))
            {
                if (DT.Rows.Count > 0)
                {
                    row = DT.Rows[0];

                    AnswerCount = Convert.ToInt64(row["ColorID"].ToString());
                    Divisor = Convert.ToInt64(row["UserID"].ToString());
                }
            }
        }

        #endregion
    }
}

